package com.mrd.gtimp.base.action;

import com.mrd.constant.MsgConstants;
import com.mrd.constant.WebPathConstants;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.config.web.json.annotation.JField;
import com.mrd.framework.config.web.json.annotation.Json;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.gtimp.base.entity.Module;
import com.mrd.gtimp.base.entity.Permissions;
import com.mrd.gtimp.base.repository.ModuleDao;
import com.mrd.gtimp.base.service.ModuleBiz;
import com.mrd.gtimp.base.service.PermissionsBiz;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import javax.annotation.Resource;

/**
 * <p>
 * application name: gtimp
 * <p>
 * copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author qiul
 */

@Controller
@RequestMapping("/base")
public class ModuleAction {
	private static final long	serialVersionUID	= 1L;
	@Resource
	private ModuleBiz			moduleBiz;
	@Resource
	private PermissionsBiz		permissionsBiz;

	private Permissions			permissions;

	private Module				module;

	private List<Module>		modules;

	private List<Permissions>	permissionses;

	@Resource
	private ModuleDao			moduleDao;

	/**
	 * 页面主入口
	 */
	@RequestMapping("/mod-main")
	public String main(Model model) {
		// title = "权限管理";
		// left = "mod-tree.do";
		// // right = "mod-pms.do";
		// right = "";
		model.addAttribute("title", "模块管理");
		model.addAttribute("left", "mod-tree");
		model.addAttribute("right", "");
		return WebPathConstants.FRAME_WEB_PATH;
	}

	/**
	 * 栏目树页面入口
	 */
	@RequestMapping("/mod-tree")
	public String tree() {
		return "/gtimp/base/role/module/mtree";
	}

	/**
	 * 权限入口
	 */
	@RequestMapping("/mod-pms")
	public String pms() {
		return "/gtimp/base/role/module/pms-manage";
	}

	/**
	 * 模块添加或修改入口
	 */
	@RequestMapping("/mod-manage")
	public String manage() {

		return "/gtimp/base/role/module/mod-save";
	}

	/**
	 * 修改模块前获取模块数据
	 */
	@RequestMapping("/mod-modBeUpdate")
	@ResponseBody
	public Module modBeUpdate(Module module) {
		try {
			if(module != null && StringUtils.isNotEmpty(module.getId())) {
				module = moduleBiz.findEntityById(module.getId());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return module;
	}

	/**
	 * 权限添加入口
	 */
	@RequestMapping("/mod-peradd")
	public String peradd() {
		return "/gtimp/base/role/module/per-save";
	}

	/**
	 * 权限修改入口
	 */
	@RequestMapping("/mod-perupdate")
	public String perupdate() {
		return "/gtimp/base/role/module/per-save";
	}

	/**
	 * 权限修改前，获取权限内容
	 */
	@RequestMapping("/mod-perBeUpdate")
	public String perBeUpdate(Permissions permissions) {
		try {
			if(permissions != null && StringUtils.isNotEmpty(permissions.getId())) {
				permissions = permissionsBiz.findEntityById(permissions.getId());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/gtimp/base/role/module/per-save";
	}

	@RequestMapping("/mod-perSave")
	@ResponseBody
	public Result<String> perSave(Permissions permissions) {
		Result msg;
		msg = Result.newFailInstance(MsgConstants.SAVE_FAIL);
		try {
			if(permissions == null || StringUtils.isEmpty(permissions.getId())) {
				permissions.setId(null);
			}
			permissionsBiz.saveOrupdateEntity(permissions);
			msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return msg;
	}

	/**
	 * 获取树数据
	 */
	@RequestMapping("/mod-treedata")
	@ResponseBody
	public List<Module> treedata(Module module) {
		try {
			if(module == null || StringUtils.isEmpty(module.getId())) {
				return moduleDao.findByParentIsNullOrderByOrderAscNameAsc();
			} else {
				return moduleDao.findByParentIdOrderByOrderAscNameAsc(module.getId());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 获取权限数据
	 */
	@RequestMapping("/mod-pdata")
	@ResponseBody
	@Json({@JField("id"), @JField("name"), @JField("pid"), @JField("type"), @JField("disable"),
	        @JField("label"), @JField("buttonId"), @JField("click") })
	public List<Permissions> pdata(Module module,Jqgrid jqgrid) {
		try {
			if(module != null && StringUtils.isNotEmpty(module.getId())) {
				jqgrid.addRule(new SearchRule("id", SearchRuleOption.eq, module.getId()));
				if(jqgrid.getOrder() == null) {
					jqgrid.addOrder("type");
				}
				Page<Permissions> pageResult = permissionsBiz.findByJqgrid(jqgrid);
				if(pageResult != null) {
					return pageResult.getContent();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 保存模块
	 */
	@RequestMapping("/mod-modsave")
	@ResponseBody
	public Result modsave(Module module) {
		Result msg;
		msg = Result.newFailInstance(MsgConstants.SAVE_FAIL);
		try {
			if(StringUtils.isEmpty(module.getId())) {
				module.setId(null);
			}
			if(module.getParent() != null && StringUtils.isEmpty(module.getParent().getId())) {
				module.setParent(null);
			}
			moduleBiz.saveOrupdateEntity(module);
			msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return msg;
	}

	/**
	 * 删除权限
	 */
	@RequestMapping("/mod-perdel")
	@ResponseBody
	public Result perdel(Permissions permissions) {
		Result msg;
		msg = Result.newFailInstance(MsgConstants.SAVE_FAIL);
		try {
			if(permissions != null && StringUtils.isNotEmpty(permissions.getId())) {
				permissionsBiz.delete(permissions);
				msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return msg;
	}

	/**
	 * 修改模块前获取模块数据
	 */
	@RequestMapping("/mod-modbesave")
	public String modbesave() {
		return "update";
	}

	@Resource
	public void setModuleBiz(ModuleBiz moduleBiz) {
		this.moduleBiz = moduleBiz;
	}

	public Module getModule() {
		return module;
	}

	public void setModule(Module module) {
		this.module = module;
	}

	public List<Module> getModules() {
		return modules;
	}

	public void setModules(List<Module> modules) {
		this.modules = modules;
	}

	public List<Permissions> getPermissionses() {
		return permissionses;
	}

	public void setPermissionses(List<Permissions> permissionses) {
		this.permissionses = permissionses;
	}

	public Permissions getPermissions() {
		return permissions;
	}

	public void setPermissions(Permissions permissions) {
		this.permissions = permissions;
	}

}
